﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Face8280WebSocketServer.DB.User;
using Face8280WebSocketServer.Interface;
using Microsoft.EntityFrameworkCore;

namespace Face8280WebSocketServer.Service
{
    /// <summary>
    /// 网址管理员操作服务
    /// </summary>
    public class UserService : BaseService, IUserService
    {

        public UserService(DbContext context) : base(context)
        {

        }

        public UserDetail GetCurrentUser()
        {
            return CurrentUser;
        }

        public DbSet<UserDetail> GetUserDBSet()
        {
            return this.Context.Set<UserDetail>();
        }



        public UserDetail UserLogin(string username, string password)
        {
            //var u = new UserDetail();
            //u.UserID = 1;
            //u.LogTime = DateTime.Now;
            //var db = this.Context.Set<UserDetail>();
            //db.Attach(u);
            //var ent = this.Context.Entry<UserDetail>(u);
            //var b = ent.Property("LogTime").IsModified;
            //this.Context.SaveChanges();
           // var sids = string.Join(",",)

            var user = Query<UserDetail>(x => x.UserName == username).AsTracking().FirstOrDefault();
            if (user == null)
            {
                throw new ServiceException(1, "没有找到此用户！");
            }
            if (user.UserName.Equals(username) && user.UserPassword.Equals(password))//应该数据库
            {
                user.LogTime = DateTime.Now;
                user.OnlineTime = DateTime.Now;

                CurrentUser = user;
                AddUserLog("登陆", "通过UI界面登陆");
                Commit();//更新到数据库
                return user;
            }
            else
            {
                throw new ServiceException(2, "用户名或密码错误！");
            }
        }


        /// <summary>
        /// 更新用户在线时间
        /// </summary>
        /// <returns></returns>
        public UserDetail UpdateUserOnlineTime(int id)
        {
            var user = Find<UserDetail>(id);

            if (user != null)
            {
                user.OnlineTime = DateTime.Now;
                this.Commit();
                return user;
            }
            else
            {
                throw new ServiceException(1, "用户已失效！");
            }
        }

        public void ClearLogs()
        {
            Excute("Delete from UserLog");
            AddUserLog("操作员管理", "清空操作员日志");
            Commit();
        }



        public void AddUser(UserDetail user)
        {

            AddUserLog("管理员管理", $"添加管理员：{user.UserName},身份：{user.Role},电话：{user.Phone}");
            Insert(user);
        }


        public bool UpdateUser(UserDetail newuser)
        {
            var db = this.Context.Set<UserDetail>();
            UserDetail User = db.Find(newuser.UserID);
            if (User == null) return false;
            
            User.Role = newuser.Role;
            User.Phone = newuser.Phone;
            if (!string.IsNullOrWhiteSpace(newuser.UserPassword))
                User.UserPassword = newuser.UserPassword;

            AddUserLog("管理员管理", $"修改管理员：{User.UserName},身份：{User.Role},电话：{User.Phone}");
            Commit();
            return true;
        }


        public void DeleteUsers(List<int> userids)
        {
            var db = this.Context.Set<UserDetail>();
            var users = from x in db
                        where userids.Contains(x.UserID)
                        select new UserDetail()
                        {
                            UserID = x.UserID,
                            UserName = x.UserName,
                            Role = x.Role,
                            Phone = x.Phone
                        };
            users = users.AsTracking();
            foreach (var user in users)
            {
                AddUserLog("管理员管理", $"删除管理员：{user.UserName},身份：{user.Role},电话：{user.Phone}");

            }
            db.RemoveRange(users);
            //var Delusers = par.UserIDs.Select(p => new UserDetail() { UserID = p });

            //Delete(Delusers);
            Commit();
        }

    }
}
